package com.ypx.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ypx.controller.CarchargeController;
import com.ypx.dao.*;
import com.ypx.model.Owner;
import com.ypx.service.IOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kappy
 * @since 2020-11-08
 */
@Service
public class OwnerServiceImpl extends ServiceImpl<OwnerMapper, Owner> implements IOwnerService {

    @Autowired
    private OwnerMapper ownerMapper;
    @Autowired
    private CarchargeMapper carchargeMapper;
    @Autowired
    private ParkingMapper parkingMapper;
    @Autowired
    private ClockinnewMapper clockinnewMapper;
    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private ComplaintMapper complaintMapper;
    @Autowired
    private RecordsMapper recordsMapper;
    @Autowired
    private PropertyInfoMapper propertyInfoMapper;
    @Autowired
    private HouseMapper houseMapper;

    @Override
    public PageInfo<Owner> findOwnerAll(int page, int pagesize, Owner owner) {
        PageHelper.startPage(page,pagesize);
        List<Owner> list=ownerMapper.queryOwnerAll(owner);
        PageInfo<Owner> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public IPage<Owner> findListByPage(Integer page, Integer pageCount){
        IPage<Owner> wherePage = new Page<>(page, pageCount);
        Owner where = new Owner();

        return   baseMapper.selectPage(wherePage, Wrappers.query(where));
    }

    @Override
    public Owner queryOwnerByName(String username) {
        return ownerMapper.queryOwnerByName(username);
    }

    @Override
    public int add(Owner owner){
        return baseMapper.insert(owner);
    }

    @Override
    public int delete(Long id){
        return baseMapper.deleteById(id);
    }

    @Override
    public int updateData(Owner owner){
        return baseMapper.updateById(owner);
    }

    @Override
    public Owner findById(Integer id){
        return  ownerMapper.queryById(id);
    }

    @Override
    public void deleteOwner(Integer id) {
        if (id!=null){
            carchargeMapper.deleteByOwnerId(id);
            repairMapper.deleteByOwnerId(id);
            complaintMapper.deleteByOwnerId(id);
            clockinnewMapper.deleteByOwnerId(id);
            parkingMapper.deleteByOwnerId(id);
            Owner owner = ownerMapper.queryById(id);
            if (owner.getHouseId()!=null){
                //退房
                houseMapper.updateStatusById(0, owner.getHouseId());
                recordsMapper.deleteByHouseId(owner.getHouseId());
                propertyInfoMapper.deleteByHouseId(owner.getHouseId());
            }
            ownerMapper.deleteById(id);
        }
    }
}
